package com.local.coupon.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

    @Value("${rabbitmq.used-user-coupon-record-exchange}")
    private String usedUserCouponRecordExchange;

    @Value("${rabbitmq.used-user-coupon-record-queue}")
    private String usedUserCouponRecordQueue;

    @Value("${rabbitmq.used-user-coupon-record-routingkey}")
    private String usedUserCouponRecordRoutingKey;

    /**
     * 声明交换机
     * @return
     */
    @Bean
    public TopicExchange usedUserCouponRecordExchange() {
        return new TopicExchange(this.usedUserCouponRecordExchange);
    }


    /**
     * 声明队列
     * @return
     */
    @Bean
    public Queue usedUserCouponRecordQueue() {
        return new Queue(this.usedUserCouponRecordQueue);
    }


    /**
     * 声明绑定关系
     * @param usedUserCouponRecordQueue
     * @param usedUserCouponRecordExchange
     * @return
     */
    @Bean
    public Binding bindExchange(Queue usedUserCouponRecordQueue, TopicExchange usedUserCouponRecordExchange) {
        return BindingBuilder.bind(usedUserCouponRecordQueue)
                .to(usedUserCouponRecordExchange)
                .with(this.usedUserCouponRecordRoutingKey);
    }
}
